package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: Dream_die
 * @Date: 2020/4/15 23:27
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    //分页查询
    @RequestMapping("/findPage")
    public Result findPages(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult page = roleService.PageQuery(queryPageBean);
            return new Result(true, "查询角色信息成功", page);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色信息失败", "网络繁忙,请稍后重试!!");
        }
    }

    //新增角色
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds) {
        try {
            roleService.add(role, permissionIds);
            return new Result(true,"添加角色信息成功！");
        } catch (Exception e) {
            return new Result(false, "添加角色信息失败！", e.getMessage());
        }
    }

    //查询所有角色信息
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Role> roles = roleService.findAll();
            return new Result(true, "查询角色信息成功",roles);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询角色信息失败！","网络繁忙，请稍后再试！");
        }
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Role role = roleService.findById(id);
            return new Result(true, "查询角色信息成功",role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询角色信息失败！","网络繁忙，请稍后再试！");
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleService.delete(id);
            return new Result(true, "删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败！","网络繁忙，请稍后再试！");
        }
    }

    @RequestMapping("/update")
    public Result update(@RequestBody Role role,Integer[] permissionIds){
        try {
            roleService.update(role,permissionIds);
            return new Result(true, "删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败！","网络繁忙，请稍后再试！");
        }
    }

    @RequestMapping("/findPermissionIdsByUserId")
    public Result findPermissionIdsByUserId(Integer id){
        try {
            List<Integer> Permissions = roleService.findPermissionIdsByUserId(id);
            return new Result(true, "查询权限信息成功",Permissions);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询权限信息失败！","网络繁忙，请稍后再试！");
        }
    }

}
